package com.lleice.web.test;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import com.lleice.common.utils.JacksonUtil;
import com.lleice.service.entity.sys.SysUser;
import com.fasterxml.jackson.databind.JsonNode;

/**
 * jackson工具类例子
 * @author lizihao
 *
 */
public class JacksonDemo {

	public static void main(String[] args) throws Exception {
		//对象转json字符串
		SysUser u = new SysUser();
		u.setCreateTime(LocalDateTime.now());
		u.setId(23L);
		u.setIsEnable(1);
		u.setUserAccount("jackson");
		System.out.println("对象转json字符串:"+JacksonUtil.to(u));
		
		
		//集合转json字符串
		SysUser u1 = new SysUser();
		u1.setCreateTime(LocalDateTime.now());
		u1.setId(23L);
		u1.setIsEnable(1);
		u1.setUserAccount("jackson11");
		
		SysUser u2 = new SysUser();
		u2.setCreateTime(LocalDateTime.now());
		u2.setId(23L);
		u2.setIsEnable(1);
		u2.setUserAccount("jackson22");
		List<SysUser> listUser = new ArrayList<SysUser>();
		listUser.add(u);listUser.add(u1);listUser.add(u2);
		System.out.println("集合转json字符串:"+JacksonUtil.to(listUser));
		
		//json字符串转集合
		String arrayStr = "[{\"id\":23,\"createTime\":\"2020-12-16 17:53:35\",\"isEnable\":1,\"userAccount\":\"jackson\"},{\"id\":23,\"createTime\":\"2020-12-16 17:53:35\",\"isEnable\":1,\"userAccount\":\"jackson11\"},{\"id\":23,\"createTime\":\"2020-12-16 17:53:35\",\"isEnable\":1,\"userAccount\":\"jackson22\"}]";
		List<SysUser> parseList = JacksonUtil.fromList(arrayStr, SysUser.class);
		parseList.forEach(a ->{
			System.out.println("json字符串转集合"+a.getUserAccount());
		});
		
		//json字符串转jsonobject
		String objectStr = "{\"id\":23,\"createTime\":\"2020-12-16 17:55:21\",\"isEnable\":1,\"userAccount\":\"jackson\"}";
		JsonNode jsonNode = JacksonUtil.getAsJsonObject(objectStr);
		System.out.println("json字符串转jsonobject"+jsonNode.get("id"));
		
		//json字符串转jsonobject(复杂对象的某个属性)
		String objectStr2 = "{\"id\":1,\"createTime\":\"2020-11-18 11:00:43\",\"updateTime\":\"2020-11-18 11:00:43\",\"isEnable\":0,\"isDelete\":0,\"userAccount\":\"admin\",\"userName\":\"超级管理员\",\"userPassword\":\"d8084a9d5caf81b92895b12b6bdec371\",\"userSalt\":\"sds232\",\"dataScope\":3,\"resourceList\":[{\"id\":\"1\",\"resourceCode\":\"index:\",\"resourceName\":\"首页\",\"resourcePath\":\"/index\",\"resourceType\":0,\"parentId\":\"0\",\"children\":[{\"id\":\"2\",\"resourceCode\":\"index:list\",\"resourceName\":\"首页查询\",\"resourcePath\":\"/index/list\",\"resourceType\":1,\"parentId\":\"1\",\"children\":[]}]},{\"id\":\"3\",\"resourceCode\":\"user:\",\"resourceName\":\"用户管理\",\"resourcePath\":\"/user\",\"resourceType\":0,\"parentId\":\"0\",\"children\":[]}],\"permissionList\":[\"index:\",\"index:list\",\"user:\"],\"token\":\"1919646c-8008-4daa-992a-75c7853b1543\",\"departmentList\":[\"1\",\"4\",\"1331175419877015554\"]}";
		JsonNode jsonNode2 = JacksonUtil.getAsJsonObject(objectStr2, "permissionList");
		List<String> permissionList = JacksonUtil.fromList(jsonNode2.toString(), String.class);
		permissionList.forEach(a ->{
			System.out.println("json字符串转jsonobject(复杂对象的某个属性)"+a);
		});
		
	}
}
